<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="条例名称" prop="criteriaName">
        <el-input v-model="queryParams.criteriaName" placeholder="请输入条例名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['bidding:criteria:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['bidding:criteria:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['bidding:criteria:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['bidding:criteria:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="categoryQt" @selection-change="handleSelectionChange" max-height="434">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="条例名称" fixed="left" width="150" align="center" prop="criteriaName" />
      <el-table-column label="评标规则" align="center">
        <el-table-column label="评估项" width="100" align="left">
          <template slot-scope="scope">
              <div v-for="value in scope.row.pcmEvaluationRuleList" :key="value.evaluationRuleId">
                {{value.evaluationItem}}
              </div>
          </template>
        </el-table-column>
        <el-table-column label="评分详情及标准" align="left" width="180" >
          <template slot-scope="scope">
              <div v-for="value in scope.row.pcmEvaluationRuleList" :key="value.evaluationRuleId">
                {{value.scoreDetailsAndStandards}}
              </div>
          </template>
        </el-table-column>
        <el-table-column label="最高分" align="left" >
          <template slot-scope="scope">
              <div v-for="value in scope.row.pcmEvaluationRuleList" :key="value.evaluationRuleId">
                {{value.maximumScore}}
              </div>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="满分" fixed="right" align="center" prop="sumScore" />
      <el-table-column label="提交人" fixed="right" align="center" prop="nickName" />
      <el-table-column label="提交时间" fixed="right" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitData, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" width="180" fixed="right">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateData, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['bidding:criteria:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['bidding:criteria:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改评标条例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="条例名称" prop="criteriaName">
          <el-input v-model="form.criteriaName" placeholder="请输入条例名称" />
        </el-form-item>
        <el-divider content-position="center">评标规则信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPcmEvaluationRule">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePcmEvaluationRule">删除
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="pcmEvaluationRuleList" :row-class-name="rowPcmEvaluationRuleIndex"
          @selection-change="handlePcmEvaluationRuleSelectionChange" ref="pcmEvaluationRule">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="评估项" prop="evaluationItem" width="190">
            <template slot-scope="scope">
              <el-input v-model="scope.row.evaluationItem" placeholder="请输入评估项" />
            </template>
          </el-table-column>

          <el-table-column label="评分详情及标准" prop="scoreDetailsAndStandards" width="190">
            <template slot-scope="scope">
              <el-input v-model="scope.row.scoreDetailsAndStandards" placeholder="请输入评分详情及标准" />
            </template>
          </el-table-column>
          <el-table-column label="最高分" prop="maximumScore" width="160">
            <template slot-scope="scope">
              <el-input v-model="scope.row.maximumScore" placeholder="请输入最高分" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCriteria,
    getCriteria,
    delCriteria,
    addCriteria,
    updateCriteria
  } from "@/api/bidding/criteria";

  export default {
    name: "Criteria",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        evaluationCriteriaNames:[],
        // 子表选中数据
        checkedPcmEvaluationRule: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 评标条例表格数据
        criteriaList: [],
        // 评标规则表格数据
        pcmEvaluationRuleList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 5,
          criteriaName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          criteriaName: [{
            required: true,
            message: "条例名称不能为空",
            trigger: "blur"
          }],
          guiRule: [{
            required: true,
            message: "评标规则不能为空",
            trigger: "blur"
          }],
        },
      };
    },
    created() {
      this.getList();
    },
    computed: {
      categoryQt() {
        let lala = [];
        this.criteriaList.forEach(item => {
          var sumMaximumScore = 0;
          item.pcmEvaluationRuleList.forEach(data => {
            sumMaximumScore = sumMaximumScore + data.maximumScore;
          });
          if (item.hasOwnProperty('sumScore')) {
            item.sumScore = sumMaximumScore;
          } else {
            this.$set(item, 'sumScore', sumMaximumScore);
          }
          lala.push(item);
        });

        return lala;
      }
    },
    methods: {
      /** 查询评标条例列表 */
      getList() {
        this.loading = true;
        listCriteria(this.queryParams).then(response => {
          this.criteriaList = response.rows;
          this.total=response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          evaluationCriteriaId: null,
          criteriaName: null,
        };
        this.pcmEvaluationRuleList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.evaluationCriteriaId)
        this.evaluationCriteriaNames = selection.map(item => item.criteriaName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加评标条例";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const evaluationCriteriaId = row.evaluationCriteriaId || this.ids
        getCriteria(evaluationCriteriaId).then(response => {
          this.form = response.data;
          this.pcmEvaluationRuleList = response.data.pcmEvaluationRuleList;
          this.open = true;
          this.title = "修改评标条例";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.pcmEvaluationRuleList = this.pcmEvaluationRuleList;
            if (this.form.evaluationCriteriaId != null) {
              updateCriteria(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCriteria(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const evaluationCriteriaIds = row.evaluationCriteriaId || this.ids;
        var yufa = "是否确认删除评标条例名称为";
        var evaluationCriteriaName = row.criteriaName || this.evaluationCriteriaNames
        if(evaluationCriteriaName.length>0){
          yufa = "是否确认删除多组评标条例?"
          evaluationCriteriaName = "";
        }
        this.$modal.confirm('"'+yufa+'"+"' + evaluationCriteriaName + '"的数据项？').then(function() {
          return delCriteria(evaluationCriteriaIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 评标规则序号 */
      rowPcmEvaluationRuleIndex({
        row,
        rowIndex
      }) {
        row.index = rowIndex + 1;
      },
      /** 评标规则添加按钮操作 */
      handleAddPcmEvaluationRule() {
        let obj = {};
        obj.evaluationItem = "";
        obj.scoreDetailsAndStandards = "";
        obj.maximumScore = "";
        this.pcmEvaluationRuleList.push(obj);
      },
      /** 评标规则删除按钮操作 */
      handleDeletePcmEvaluationRule() {
        if (this.checkedPcmEvaluationRule.length == 0) {
          this.$modal.msgError("请先选择要删除的评标规则数据");
        } else {
          const pcmEvaluationRuleList = this.pcmEvaluationRuleList;
          const checkedPcmEvaluationRule = this.checkedPcmEvaluationRule;
          this.pcmEvaluationRuleList = pcmEvaluationRuleList.filter(function(item) {
            return checkedPcmEvaluationRule.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handlePcmEvaluationRuleSelectionChange(selection) {
        this.checkedPcmEvaluationRule = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('bidding/criteria/export', {
          ...this.queryParams
        }, `criteria_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style scoped>
  ::v-deep .el-table .el-table__header-wrapper th, .el-table .el-table__fixed-header-wrapper th{
    height: 20px;
  }
  ::v-deep .el-table--medium .el-table__cell{
    padding: 4px 0;
  }
  ::v-deep .cell{
    padding: 0;
  }
  ::v-deep .cell div{
    padding: 6px 0 1px 10px;
    border-bottom:1px solid #DFE6EC;
  }
  ::v-deep .cell div:nth-last-child(1){
    border-bottom: 0;
  }
</style>
